## Figure K.1: Effect of Lobbying on Donations, Senate: -------------------------

## Instructions ----------------------------------------------------------------
# Step 1: Adjust DATA_DIR to where data is is located
# Step 2: Adjust OUT_DIR to where output folder is
# Step 3: Run entire script
## setup -----------------------------------------------------------------------


# clean slate
rm(list = ls())
# Need to install to read/use PanelEstimate Objects
# devtools::install_github("insongkim/PanelMatch",ref = "se_comparison")


# LOAD PACKAGES
library(PanelMatch)
library(dplyr)
options(scipen=999)

# ### SUBSETTING AND PATH
# naics_sub <- c("31", "32", "33") #### <- set industries to be analyzed
# chamber_sub <- c("senate") #### <- set chamber to be analyzed
# path <- "../data/don_lob_quarter_20210525_senate.csv" ### set path to be analyzes
# #outcome_select <- c("don_any_bin", "don_pac_bin", "don_emp_bin", "don_ceo_bin", "don_gov_bin")
# outcome_select <- c("don_any_bin")
# treatment_select <- c("lob_spon_bin", "lob_cosp_only_bin", "lob_comm_bin", "lob_any_bin")
# 
# 
# #### SET ANALYSIS PARAMETERS ######
# ## refinement_method <- "CBPS.weight" # set refinement
# refinement_method <- "mahalanobis" # set refinement
# size_match <- 10 # size of matched set
# lags_match_dv <- "1:8" # DV lags to match on. Set 8 so that cover electoral cycle 
# lags_leads <- 8 # Lags/Leads in PanelMatch. Set 8 so that cover electoral cycle 
# covariates_match <- "l_sale + l_emp" # other covariates to match on
# ## exact_match <- c("naics3", "govtrack_id") # exact matchin
# exact_match <- c("govtrack_id") # exact matchin
# outcomes <- c("don_any_bin", "don_pac_bin", "don_ceo_bin", "don_gov_bin", "don_emp_bin") # treatment variable
# outcome_names <- c("AnyDon", "PacDon", "CeoDon", "GovDon", "EmpDon") # treatment variable
# treatments <- c("lob_spon_bin", "lob_cosp_only_bin", "lob_comm_bin", "lob_any_bin")
# treatname <- c("SpLob", "CoSpLob", "CommLob", "AnyLob")
# chamber_names <- c("Senate", "House")
# 
# 
# #### CREATE A DF WITH ANALYSIS PAREMTERS ######
# specs_df <- data.frame(chamber = rep(rep(c("senate", "house"), each = 5), 4),
#                        chamber_names = rep(rep(chamber_names, each = 5), 4),
#                        treatments = rep(treatments, each = 10),
#                        treatname = rep(treatname, each = 10),
#                        outcomes = rep(outcomes, 8),
#                        outcome_names = rep(outcome_names, 8),
#                        refinement_method = rep(refinement_method, 40),
#                        size_match = rep(size_match, 40),
#                        exact_match1 = rep(exact_match[1], 40),
#                        covariate_match = rep(covariates_match, 40),
#                        lags_match_dv = rep(lags_match_dv, 40),
#                        stringsAsFactors = FALSE)
# 
# specs_df <- specs_df %>% filter(chamber %in% chamber_sub, outcomes %in% outcome_select) # subset by chosen chamber
# 
# # LOAD DATA, SUBSET, and keep only variables needed
# don_lob <- readr::read_csv(path)%>%
#   filter(naics2 %in% naics_sub, chamber %in% chamber_sub) %>%
#   mutate(l_sale = log(sale+1), l_emp = log(emp+1)) %>%
#   select(gvkey_govtrack_id, gvkey, govtrack_id, year, year_quarter_int, naics2, chamber, # IDs
#          lob_any_bin, lob_spon_bin, lob_cosp_only_bin, lob_comm_bin, # outcomes
#          don_any_bin, don_pac_bin, don_emp_bin, don_ceo_bin, don_gov_bin, # outcomes, reverse analysis
#          l_sale, l_emp # firm covariates
#   ) %>% # some recodings necessary
#   mutate(gvkey_govtrack_id_numeric = as.integer(as.factor(gvkey_govtrack_id)),
#          year_quarter_int = as.integer(year_quarter_int)) #
# 
# 
# 
# # need to set as DF, if not PM breaks sometimes
# don_lob <- as.data.frame(don_lob)
# 
# 
# # print number of rows
# print(paste0("using dataset with ", nrow(don_lob), " rows."))
# 
# 
# 
# # start loop across all outcomes
# for (i in 1:dim(specs_df)[1]) {
#   
#   # set up the respective outcome names for plot titles
#   outcome_name <- specs_df$outcome_names[i] # change with outcome used
#   chamber_name <- specs_df$chamber_names[i] #
#   treatment_name <- specs_df$treatname[i]
#   
#   # crete covariate formulat for PanelMatch()
#   covs_formula <- paste0("~ I(lag(", specs_df$outcomes[i], ", ", lags_match_dv, ")) + ", specs_df$covariate_match[i])
#   
#   
#   
#   ##### Run Panelmatch
#   Matches.lobby <- PanelMatch(lag = lags_leads, lead = 0:lags_leads, time.id = "year_quarter_int", unit.id = "gvkey_govtrack_id_numeric",
#                               treatment = specs_df$treatments[i], match.missing = TRUE,
#                               outcome.var = specs_df$outcomes[i], qoi = "att",
#                               covs.formula = as.formula(covs_formula),
#                               exact.match.variables = c(specs_df$exact_match1[i]),
#                               forbid.treatment.reversal = FALSE,
#                               restrict.control.period = lags_leads,
#                               size.match = specs_df$size_match[i],
#                               data = don_lob,
#                               refinement.method = specs_df$refinement_method[i])
#   
#   output_name <- paste0("manuf_quart_revcause_", specs_df$chamber[i], "_cbps_", 
#                         specs_df$treatname[i], "_" , specs_df$outcome_names[i])
#   
#   
#   # Save PanelMatch Object
#   save(Matches.lobby, file = paste0("../output/MSets", output_name))
#   print(paste0(i,". ", "saved **Matched Sets** object to ", "../output/MSets_", output_name))
#   gc()
#   
#   
#   # Run PanelEstimate
#   PM.lobby <- PanelEstimate(sets = Matches.lobby, data = don_lob, se.method = "analytical")
#   
#   # Save PanelEstimate Object
#   save(PM.lobby, file = paste0("../output/EstObj_", output_name))
#   print(paste0(i,". ", "saved **Panel Estimate** object to ", "../output/EstObj_", output_name))
#   gc()
#   
#   
#   #### Remove PanelMatch and PanelEstimate objects at th end of each run
#   rm(PM.lobby)
#   rm(Matches.lobby)
#   gc()
#   
#   
# }


## library
library(dplyr)
library(data.table)
library(PanelMatch)
library(kableExtra)


library(PanelMatch)
DATA_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package/data/figK1"
OUT_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package/output"

file_list <- list.files(DATA_DIR) 
file_list <- file_list[which(grepl("EstimateObj", x = file_list, fixed = TRUE))]
file_list_sub <- rev(file.path(DATA_DIR, file_list))[c(2,4,1,3)]

treat <- c("Any Lobbying", "Lobby Sponsor", "Lobby Cosponsor", "Lobby Committee")
ylab <- c("Estimated Effects of Donation Lobbying on Probability to Donate in:")


# create table
library(kableExtra)
options(scipen = 999)
n_row <- 8
tab <- data.frame(lob_type = rep(NA, n_row),
                  `t+0` = rep(NA, n_row),
                  `t+1` = rep(NA, n_row),
                  `t+2` = rep(NA, n_row),
                  `t+3` = rep(NA, n_row),
                  `t+4` = rep(NA, n_row),
                  `t+5` = rep(NA, n_row),
                  `t+6` = rep(NA, n_row),
                  `t+7` = rep(NA, n_row))

load(file_list_sub[1])
tab[1,] <-  c(paste0("\\textit{", treat[1], "}"), round(PM.lobby$estimates[1:8],4)) 
tab$t.0[2] <- c(paste0("(",round(PM.lobby$standard.error[1], 4), ")"))
tab$t.1[2] <- c(paste0("(",round(PM.lobby$standard.error[2], 4), ")"))
tab$t.2[2] <- c(paste0("(",round(PM.lobby$standard.error[3], 4), ")"))
tab$t.3[2] <- c(paste0("(",round(PM.lobby$standard.error[4], 4), ")"))
tab$t.4[2] <- c(paste0("(",round(PM.lobby$standard.error[5], 4), ")"))
tab$t.5[2] <- c(paste0("(",round(PM.lobby$standard.error[6], 4), ")"))
tab$t.6[2] <- c(paste0("(",round(PM.lobby$standard.error[7], 4), ")"))
tab$t.7[2] <- c(paste0("(",round(PM.lobby$standard.error[8], 4), ")"))

load(file_list_sub[2])
tab[3,] <-  c(paste0("\\textit{", treat[2], "}"), round(PM.lobby$estimates[1:8],4)) 
tab$t.0[4] <- c(paste0("(",round(PM.lobby$standard.error[1], 4), ")"))
tab$t.1[4] <- c(paste0("(",round(PM.lobby$standard.error[2], 4), ")"))
tab$t.2[4] <- c(paste0("(",round(PM.lobby$standard.error[3], 4), ")"))
tab$t.3[4] <- c(paste0("(",round(PM.lobby$standard.error[4], 4), ")"))
tab$t.4[4] <- c(paste0("(",round(PM.lobby$standard.error[5], 4), ")"))
tab$t.5[4] <- c(paste0("(",round(PM.lobby$standard.error[6], 4), ")"))
tab$t.6[4] <- c(paste0("(",round(PM.lobby$standard.error[7], 4), ")"))
tab$t.7[4] <- c(paste0("(",round(PM.lobby$standard.error[8], 4), ")"))

load(file_list_sub[3])
tab[5,] <-  c(paste0("\\textit{", treat[3], "}"), round(PM.lobby$estimates[1:8],4)) 
tab$t.0[6] <- c(paste0("(",round(PM.lobby$standard.error[1], 4), ")"))
tab$t.1[6] <- c(paste0("(",round(PM.lobby$standard.error[2], 4), ")"))
tab$t.2[6] <- c(paste0("(",round(PM.lobby$standard.error[3], 4), ")"))
tab$t.3[6] <- c(paste0("(",round(PM.lobby$standard.error[4], 4), ")"))
tab$t.4[6] <- c(paste0("(",round(PM.lobby$standard.error[5], 4), ")"))
tab$t.5[6] <- c(paste0("(",round(PM.lobby$standard.error[6], 4), ")"))
tab$t.6[6] <- c(paste0("(",round(PM.lobby$standard.error[7], 4), ")"))
tab$t.7[6] <- c(paste0("(",round(PM.lobby$standard.error[8], 4), ")"))

load(file_list_sub[4])
tab[7,] <-  c(paste0("\\textit{", treat[4], "}"), round(PM.lobby$estimates[1:8],4)) 
tab$t.0[8] <- c(paste0("(",round(PM.lobby$standard.error[1], 4), ")"))
tab$t.1[8] <- c(paste0("(",round(PM.lobby$standard.error[2], 4), ")"))
tab$t.2[8] <- c(paste0("(",round(PM.lobby$standard.error[3], 4), ")"))
tab$t.3[8] <- c(paste0("(",round(PM.lobby$standard.error[4], 4), ")"))
tab$t.4[8] <- c(paste0("(",round(PM.lobby$standard.error[5], 4), ")"))
tab$t.5[8] <- c(paste0("(",round(PM.lobby$standard.error[6], 4), ")"))
tab$t.6[8] <- c(paste0("(",round(PM.lobby$standard.error[7], 4), ")"))
tab$t.7[8] <- c(paste0("(",round(PM.lobby$standard.error[8], 4), ")"))



colnames(tab) <- c("Type of Lobbying", 
                   "\\textit{t+0}", "\\textit{t+1}", 
                   "\\textit{t+2}", "\\textit{t+3}",
                   "\\textit{t+4}","\\textit{t+5}",
                   "\\textit{t+6}","\\textit{t+7}")

options(knitr.kable.NA = "") 
tab %>%
  kable(format = "latex", escape = FALSE, align = "lcccccccc", 
        caption = "\\textbf{Coefficient Estimates from Figure K.1}: This table reports the estimated coefficients and associated standard errors, computed based on a block-bootstrap procedure, which are
used to produce Figure K.1. To compute the results for each type of lobbying activity, we identified all dyads where a particular company donated to a particular Senator for the first time in at
least eight quarters. The number of observations is 12,232,896 firm-politician-quarter dyads. For
Any Lobbying, Lobby Sponsor, Lobby Cosponsor, and Lobby Committee, there are 16,860, 4,426,
13,062, and 16,411 such instances, respectively.", 
        label = "tab_k1") %>%
  add_header_above(c(" " = 1, "Estimated Effect of Donation on Probability to Lobby in:" = 7), line = FALSE) %>%
  add_header_above(c(" " = 1, "Dependent Variable:" = 7), italic = TRUE)  %>%
  footnote(general = "Std. errors in parentheses",footnote_as_chunk = TRUE) %>%
  kable_styling(full_width = TRUE) %>%
  save_kable(file.path(OUT_DIR, "table_k1.tex")) 


# clean up
rm(list = ls())
gc()


